package deprecated.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import util.DomainUtils;
import util.FormArgumentsException;
import domain.dao.interfaces.IDAOUser;
import domain.entities.impl.User;
import domain.entities.interfaces.IUser;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String login = DomainUtils.trim(request.getParameter("login"));
		String password = request.getParameter("password");
		HttpSession session = request.getSession(false);

		ApplicationContext context = util.SpringUtils.getApplicationContext();
		try {
			
			checks(request, login, password);
			
			session = request.getSession(true);
			IDAOUser daoUser = (IDAOUser) context.getBean("beanDAOUser");
			session.setAttribute(DomainUtils.CURRENT_USER_STRING, daoUser.getByLogin(login));	
//			session.setAttribute(DomainUtils.CURRENT_USER_STRING, DAOFactory.getDaoUser().getByLogin(login));
		}
		catch(Exception e) {

			System.out.println(e);
			
			if((e instanceof FormArgumentsException) == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "An error occur on the server.");
			}

			DomainUtils.keepParametersContent(request);
		}


		request.getRequestDispatcher("pages/index.jsp").forward(request, response);
	}

	private void checks(HttpServletRequest request,
			String login, String password) throws Exception{
		
		ApplicationContext context = util.SpringUtils.getApplicationContext();
		
		IDAOUser daoUser = (IDAOUser) context.getBean("beanDAOUser");
		List<IUser> users = daoUser.getAll();

		//List<User> users = DAOFactory.getDaoUser().getAll();
		HttpSession session = request.getSession(false);
		IUser foundUser = null;

		
		//Checking session
		if(session == null || users == null) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"You are not currently connected. May be the session expired.");
			throw new FormArgumentsException();
		}

		if(session.getAttribute(DomainUtils.CURRENT_USER_STRING) != null) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"You cannot realize this action because you are connected.");
			throw new FormArgumentsException();
		}


		//Checking login
		foundUser = daoUser.getByLogin(login);
		//foundUser = DAOFactory.getDaoUser().getByLogin(login);
		
		if(foundUser == null) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"The specified login and password does not match a registered user.");
			throw new FormArgumentsException();
		}

		
		//Checking password
		if(foundUser.getPassword().compareTo(password) != 0) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"The specified login and password does not match a registered user.");
			throw new FormArgumentsException();
		}
	}
}
